package com.benyanyi.viewbind.inject;

import android.view.View;

import com.benyanyi.viewbind.annotation.BindString;
import com.benyanyi.viewbind.annotation.BindView;
import com.benyanyi.viewbind.annotation.OnClick;
import com.benyanyi.viewbind.init.ViewInit;
import com.benyanyi.viewbind.listener.ViewBindClickListener;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author BenYanYi
 * @date 2018/12/26 10:31
 * @email ben@yanyi.red
 * @overview
 */
public class ViewInject {
    /**
     * 控件id反射
     */
    public static void injectFiled(ViewInit inject, Object object) {
        Class<?> aClass = object.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            BindView bindView = field.getAnnotation(BindView.class);
            if (bindView != null) {
                int value = bindView.value();
                View view = inject.find(value);
                if (view == null) {
                    throw new NullPointerException(value + " is null id");
                } else {
                    //能够注入所有修饰符
                    field.setAccessible(true);
                    try {
                        //动态注入找到view
                        field.set(object, view);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * string值反射
     */
    public static void injectString(ViewInit inject, Object object) {
        Class<?> aClass = object.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            BindString bindString = field.getAnnotation(BindString.class);
            if (bindString != null) {
                int value = bindString.value();
                String string = inject.string(value);
                field.setAccessible(true);
                try {
                    field.set(object, string);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 控件点击事件反射
     */
    public static void injectClick(ViewInit inject, Object object) {
        Class<?> aClass = object.getClass();
        Method[] methods = aClass.getDeclaredMethods();
        for (Method method : methods) {
            OnClick onClick = method.getAnnotation(OnClick.class);
            if (onClick != null) {
                int[] values = onClick.value();
                if (values.length > 0) {
                    for (int value : values) {
                        if (value == View.NO_ID) {
                            return;
                        }
                        View view = inject.find(value);
                        String netErrorMsg = onClick.netErrorMsg();
                        boolean isCheckNet = onClick.isCheckNet();
                        boolean doubleClick = onClick.isDoubleClick();
                        if (view != null) {
                            view.setOnClickListener(new ViewBindClickListener(method, object, netErrorMsg, isCheckNet, doubleClick));
                        } else {
                            throw new NullPointerException(value + "is null id");
                        }
                    }
                }
            }
        }
    }
//    #Gradle
//    export GRADLE_HOME=/Applications/Android\ Studio.app/Contents/gradle/gradle-4.1
//    export PATH=$PATH:$GRADLE_HOME/bin

//    static void injectException(ViewInit inject, Object object) {
//        Class<?> aClass = object.getClass();
//        Method[] methods = aClass.getMethods();
//        for (Method method : methods) {
//            Exceptions exceptions = method.getAnnotation(Exceptions.class);
//            if (exceptions != null) {
//
//            }
//        }
//    }
}
